{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "import evaluate\n",
    "from transformers import DataCollatorWithPadding, Trainer, TrainingArguments\n",
    "\n",
    "from MyHelper import *"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "model, tokenizer = init_model_and_tokenizer(Config.hfl_rbt3)",
   "id": "4b31e828951ef505",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "trainset, validset = init_dataset(tokenizer)",
   "id": "75d45ca4136083d8",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "multi_metrics = evaluate.combine([\"accuracy\", \"f1\", \"recall\", \"precision\"])\n",
    "\n",
    "def eval_function(rt):\n",
    "    preds, labels = rt\n",
    "    preds = preds.argmax(axis=-1)\n",
    "    return multi_metrics.compute(preds, labels)\n",
    "\n",
    "args = TrainingArguments(\n",
    "    output_dir=\"./output\",\n",
    "    logging_steps=10,\n",
    "    per_device_train_batch_size=64,\n",
    "    eval_strategy='steps',\n",
    "    eval_steps=40,\n",
    "    per_device_eval_batch_size=128,\n",
    "    save_total_limit=2,\n",
    "    report_to=\"all\"\n",
    ")\n",
    "\n",
    "trainer = Trainer(\n",
    "    model=model,\n",
    "    args=args,\n",
    "    train_dataset=trainset,\n",
    "    eval_dataset=validset,\n",
    "    compute_metrics=eval_function,\n",
    "    tokenizer=tokenizer,\n",
    "    data_collator=DataCollatorWithPadding(tokenizer=tokenizer),\n",
    ")\n"
   ],
   "id": "a645e2107f4d021b",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "trainer.train()",
   "id": "2c6ede442e6c6809",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "trainer.evaluate()",
   "id": "f6e70c97e7616163",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "trainer.evaluate(trainset)",
   "id": "b9f97c67602b4f9f",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "trainer.predict(trainset)",
   "id": "acfa9d7bea5a17cf",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "trainer.predict(validset)",
   "id": "93263c6969e959e2",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "",
   "id": "b17e94be00c48a1c",
   "outputs": [],
   "execution_count": null
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
